//
//  MDIVisitorView.swift
//  MDIWeibo
//
//  Created by 迁徙鸟 on 16/4/17.
//  Copyright © 2016年 qianxiniao. All rights reserved.
//

import UIKit
import SnapKit

class MDIVisitorView: UIView {

//    创建一个闭包
    var visitorClosure : (()->())?
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//    MARK - 监听点击事件
    @objc private func clickRegister(){
    
//        print("点击")
        
    visitorClosure?()
    
    }
    
//    MARK - 供外界调用的方法
    func setVisitorInfo(imgName: String?, title : String?){
    
        if let img = imgName, tit = title{
        
                 iconImageView.image = UIImage(named: img)
            
                 messageLabel.text = tit
            
            circleImageView.hidden = true
        }else{
        
//            表示首页
        circleImageViewTransform()
        }
        

    }

//    MARK - 更新UI
    private func setUpUI(){

        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        
//    MARK - 1、添加控件
        addSubview(circleImageView)
        addSubview(coverImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        
//    MARK - 2、设置frame
        
//        圆圈
        circleImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
//       遮盖
        coverImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
        
//        房子
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
//        文字
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(coverImageView.snp_bottom).offset(16)
            make.centerX.equalTo(coverImageView)
            make.height.equalTo(36)
            make.width.equalTo(230)
           
//            注册按钮
            registerBtn.snp_makeConstraints(closure: { (make) -> Void in
                
                make.leading.equalTo(messageLabel)
                make.top.equalTo(messageLabel.snp_bottom).offset(20)
                make.size.equalTo(CGSize(width: 100, height: 36))
            })
        }

//        登录按钮
        loginBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(registerBtn.snp_top)
            make.right.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100, height: 36))
            
        }
        
    }
    
    
//    MARK - 圆圈旋转的方法
    private func circleImageViewTransform(){
    
    let trans = CABasicAnimation(keyPath: "transform.rotation")
//        旋转时间
    trans.duration = 20
//        旋转次数
    trans.repeatCount = MAXFLOAT
//      旋转角度
    trans.toValue = 2 * M_PI
        
        trans.removedOnCompletion = false
        
//       添加
    circleImageView.layer.addAnimation(trans, forKey: nil)
        
    }
    
//    MARK - 懒加载控件
//    1、圆圈
    lazy var circleImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
//    2、遮挡(另一种懒加载)
    private lazy var coverImageView:UIImageView = {
    
    let img = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    return img
    }()
    
//    3、房子
    private lazy var iconImageView : UIImageView = {
    
    let img = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
        return img
    }()
    
//    4、文字
    private lazy var messageLabel : UILabel = {
    
        let lab = UILabel(textColor: UIColor.grayColor(), textFont: 14)
        
        lab.text = "关注一些人，回这里看看有什么惊喜 关注一些人，回这里看看有什么惊喜"
        lab.textAlignment = .Center
        lab.numberOfLines = 0
//        lab.font = UIFont.systemFontOfSize(14)
    return lab
    }()
    
//     5、注册
    private lazy var registerBtn : UIButton = {
    
    let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "clickRegister", forControlEvents: UIControlEvents.TouchUpInside)
                
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        return btn
    }()
  
//    6、登录
    private lazy var loginBtn : UIButton = {
    
    let btn = UIButton()
        
    btn.setTitle("登录", forState: UIControlState.Normal)
    
    btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
    
    btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
     btn.addTarget(self, action: "clickRegister", forControlEvents: UIControlEvents.TouchUpInside)
    
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
    return btn
    }()
    
    
    
}
